//
//  DynamicCellContentView.m
//  fitness-iOS
//
//  Created by 卢仕彤 on 2018/1/14.
//  Copyright © 2018年 卢仕彤. All rights reserved.
//

#import "DynamicCellContentView.h"
#import "DynamicViewModel.h"
#import "NSArray+FFSafeMethod.h"
#import "FFGlobalHeader.h"

#define KLearb                  @"训练教学"
#define KStory                  @"任务故事"

static NSInteger learnWidth = 200;
static NSInteger storyWidth = 300;


@interface DynamicCellContentView()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)UICollectionViewFlowLayout *layout;
@end

@implementation DynamicCellContentView

#pragma mark ------------------------------- Setter／Getter -------------------------------
#pragma mark--展示view懒加载部分

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        _layout = layout;
        layout.minimumLineSpacing = _cellSpace;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.showsHorizontalScrollIndicator = NO;
        
        [_collectionView registerClass:[DynamicCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([DynamicCollectionViewCell class])];
        [_collectionView registerClass:[DynamicStoryViewCell class] forCellWithReuseIdentifier:NSStringFromClass([DynamicStoryViewCell class])];
        
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_collectionView];
        
    }
    return _collectionView;
}

-(void)setCellSpace:(CGFloat)cellSpace
{
    _cellSpace = cellSpace;
    _layout.minimumLineSpacing = cellSpace;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self == [super initWithFrame:frame]) {
        _cellSpace = 15;
        [self configUI];
    }
    return self;
}
-(void)configUI{
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.equalTo(self);
    }];
    
}

#pragma mark----刷新界面
-(void)setModelArray:(NSArray<DynamicDetailModel *> *)modelArray
{
    if (modelArray.count != 0) {
        _modelArray = modelArray;
        if (_collectionView) {
            [_collectionView reloadData];
        }
    }
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _modelArray.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DynamicCollectionViewCell *cell = nil;
    if ([_mainName isEqualToString:KLearb]) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([DynamicCollectionViewCell class]) forIndexPath:indexPath];
        
    }else if([_mainName isEqualToString:KStory]){
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([DynamicStoryViewCell class]) forIndexPath:indexPath];
    }
    if (!cell) {
        UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifer" forIndexPath:indexPath];
        return cell;
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        cell.model = [_modelArray ff_safeObjectAtIndex:indexPath.item];
    });
    return cell;
    
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(ff_showDidSelectItem: mainName:)]) {
        [self.delegate ff_showDidSelectItem:[_modelArray ff_safeObjectAtIndex:indexPath.item] mainName:_mainName];
    }
}
- ( UIEdgeInsets )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:( NSInteger )section {
    return UIEdgeInsetsMake ( 0, 20 , 0 , 20 );
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return [self computationsCellHeightForRowsIndexPath:indexPath];
}
#pragma mark--计算UICollectionViewCell高度
-(CGSize)computationsCellHeightForRowsIndexPath:(NSIndexPath*)indexPath
{
    if ([_mainName isEqualToString:KLearb]) {
        return CGSizeMake(learnWidth, self.bounds.size.height);
    }
    return CGSizeMake(storyWidth, self.bounds.size.height);
}


@end
